package com.angel.webapp.servlets;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.angel.architecture.exceptions.NonBusinessException;
import com.angel.common.helpers.StringHelper;
import com.angel.common.keyValue.KeyValueResult;
import com.angel.io.descriptor.ExportedFileDescriptor;
import com.angel.webapp.file.FileInformation;
import com.angel.webapp.keyValue.SearchParametersKeyValueResult;
import com.angel.webapp.manager.ExportManager;

/**
 * Servlet to download files from exports domain objects.
 *
 * @author William
 * @since 13/August/2009
 */
public class DownloadExportFileServlet extends DownloadServlet {

    protected static final String REQ_PARAMETER_FILE_PROCESSOR_NAME = "exportFileCommandName";

    protected static final String REQ_PARAMETER_SEARCH_STRATEGY_NAME = "searchStrategyName";

    protected static final String REQ_PARAMETER_ROW_PROCESSOR_NAME = "exportRowCommandName";

    protected static final String REQ_PARAMETER_PARAMETERS_NAME = "searchParameters";
    
    protected static final String REQ_PARAMETER_COLUMN_NAMES = "columnNames";
    
    protected static final String REQ_PARAMETER_COLUMN_NAMES_SEPARATOR = "columnNamesSeparator";

    protected static final long serialVersionUID = -7427467996390884097L;

    @Override
    protected FileInformation doProcess(KeyValueResult keyValueResult, final HttpServletRequest request, final HttpServletResponse response) {   	
        String searchStrategyName = keyValueResult.getResultFor(REQ_PARAMETER_SEARCH_STRATEGY_NAME).toString();
        String exportRowProcessorName = keyValueResult.getResultFor(REQ_PARAMETER_ROW_PROCESSOR_NAME).toString();
        String exportFileProcessorName = keyValueResult.getResultFor(REQ_PARAMETER_FILE_PROCESSOR_NAME).toString();
        String columnNamesString = keyValueResult.getResultFor(REQ_PARAMETER_COLUMN_NAMES).toString();
        String columnNamesSeparator = keyValueResult.getResultFor(REQ_PARAMETER_COLUMN_NAMES_SEPARATOR).toString();
        Object parametersObject = keyValueResult.getResultFor(REQ_PARAMETER_PARAMETERS_NAME);
        
        String parameters = "";
        if(parametersObject != null){
        	parameters = parametersObject.toString();
        }
        String separatorParameters = keyValueResult.getResultFor(REQ_PARAMETER_SEPARATOR_PARAMETERS).toString();

        KeyValueResult searchParameters = new SearchParametersKeyValueResult(parameters, separatorParameters);
        List<String> columnNames = StringHelper.split(columnNamesString, columnNamesSeparator);

        ExportedFileDescriptor exportedFileDescriptor = this.doExport(exportFileProcessorName, exportRowProcessorName, searchStrategyName, columnNames, searchParameters);
        FileInformation fileInformation = exportedFileDescriptor.buildFileInformation();
        
        if(StringHelper.containsAtLeast(fileInformation.getFileName()," ")){
        	throw new NonBusinessException("File name [" + fileInformation.getFileName() + "] to download [export] cannot have spaces.");
        }
        return fileInformation;
    }
    
    protected ExportedFileDescriptor doExport(String exportFileProcessorName, String exportRowProcessorName, String searchStrategyName, List<String> columnNames, KeyValueResult searchParameters){
        ExportManager exportManager = new ExportManager();
        exportManager.setExportFileCommandName(exportFileProcessorName);
        exportManager.setExportRowCommandName(exportRowProcessorName);
        exportManager.setSearchStrategyCommandName(searchStrategyName);
        exportManager.setColumnNames(columnNames);
        exportManager.validate();

        ExportedFileDescriptor exportedFileDescriptor = exportManager.export(searchParameters);    	
        return exportedFileDescriptor;
    }

	@Override
    protected void doCheckParameters(KeyValueResult keyValueResult) {
    	super.doCheckParameters(keyValueResult);
        boolean containsParameterFileProcessorName = keyValueResult.containsResultFor(REQ_PARAMETER_FILE_PROCESSOR_NAME);
        boolean containsParameterRowProcessorName = keyValueResult.containsResultFor(REQ_PARAMETER_ROW_PROCESSOR_NAME);
        boolean containsParameterSearchStrategyName = keyValueResult.containsResultFor(REQ_PARAMETER_SEARCH_STRATEGY_NAME);
        boolean containsParameterColumnNames = keyValueResult.containsResultFor(REQ_PARAMETER_COLUMN_NAMES);
        boolean containsParameterColumnNamesSeparator = keyValueResult.containsResultFor(REQ_PARAMETER_COLUMN_NAMES_SEPARATOR);

        if(	!containsParameterFileProcessorName || !containsParameterRowProcessorName || 
        	!containsParameterSearchStrategyName || !containsParameterColumnNames || 
        	!containsParameterColumnNamesSeparator
        	){
            String errorMessage = "Export process needs to be executed some parameters which are not present: " +
                    "Parameter Name: " + REQ_PARAMETER_FILE_PROCESSOR_NAME + " Included: " + containsParameterFileProcessorName +
                    "Parameter Name: " + REQ_PARAMETER_ROW_PROCESSOR_NAME + " Included: " + containsParameterRowProcessorName +
                    "Parameter Name: " + REQ_PARAMETER_SEARCH_STRATEGY_NAME + " Included: " + containsParameterSearchStrategyName +
                    "Parameter Name: " + REQ_PARAMETER_COLUMN_NAMES + " Included: " + containsParameterColumnNames +
                    "Parameter Name: " + REQ_PARAMETER_COLUMN_NAMES_SEPARATOR + " Included: " + containsParameterColumnNamesSeparator;
            throw new NonBusinessException(errorMessage);
        }
    }
}